var app = angular.module('myApp', []);

app.controller('loginController', function($rootScope,$scope,$http,$location) {
    $scope.username="";

    $scope.login = function(){
    	var formdata = {
            "email" : $scope.email,
            "password" : $scope.password,
            "yzm" : $scope.yzm
	    };

    	$http({
	        method : 'POST',
	        url : $scope.basePath + 'user/back_login',
	        data : formdata,
			headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
			transformRequest: function(obj) {
				var str = [];
				for(var p in obj){
					str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
				}
				console.log(str.join("*"));
				return str.join("&");
			}
	    }).
	    success(function(response) {
	        console.log(response);
	        var user = response.data;
	        if (response.isSuccess) {
                localStorage.setItem('userId',user.userId);
	        	localStorage.setItem('username', user.username);
	        	localStorage.setItem('type', user.type);
	        	$rootScope.loginUser = user;
	        	location.href = $scope.basePath + "user/center/";
	        }

	        alert(response.msg);
	    }).
	    error(function(response) {
	        console.log(response);
	    });
    };

    $scope.changeCode = function () {
    	console.log("changecode()");
    	src = 'yzm?tm=' + Math.random();
    }

    $scope.basePath = globalPath.getBasePath();
});